package cn.easyshipping.cloud.business.service.impl;

import cn.easyshipping.cloud.business.entity.BaseEntity;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.session.SqlSession;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class BaseService {

    protected SqlSession sqlSessionBatch(Class<? extends BaseEntity> entityClass) {
        return SqlHelper.sqlSessionBatch(entityClass);
    }

    /**
     * 获取 SqlStatement
     */
    protected String sqlStatement(Class<? extends BaseEntity> entityClass, SqlMethod sqlMethod) {
        return SqlHelper.table(entityClass).getSqlStatement(sqlMethod.getMethod());
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<? extends BaseEntity> entityList) {
        if (entityList == null || entityList.size() == 0) {
            return true;
        }
        Class<? extends BaseEntity> cls = entityList.get(0).getClass();
        String sqlStatement = this.sqlStatement(cls, SqlMethod.INSERT_ONE);
        SqlSession batchSqlSession = this.sqlSessionBatch(cls);
        Throwable var5 = null;
        int batchSize = entityList.size();

        try {
            int i = 0;

            for(Object entity : entityList) {
                batchSqlSession.insert(sqlStatement, entity);
                if (i > 0 && i % batchSize == 0) {
                    batchSqlSession.flushStatements();
                }

                i++;
            }

            batchSqlSession.flushStatements();
            return true;
        } catch (Throwable var16) {
            var5 = var16;
            throw var16;
        } finally {
            if (batchSqlSession != null) {
                if (var5 != null) {
                    try {
                        batchSqlSession.close();
                    } catch (Throwable var15) {
                        var5.addSuppressed(var15);
                    }
                } else {
                    batchSqlSession.close();
                }
            }

        }
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean updateBatch(List<? extends BaseEntity> entityList) {
        if (entityList == null || entityList.size() == 0) {
            return true;
        }
        Class<? extends BaseEntity> cls = entityList.get(0).getClass();
        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
        String keyProperty = tableInfo.getKeyProperty();
        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");

        SqlSession batchSqlSession = this.sqlSessionBatch(cls);
        String sqlStatement = this.sqlStatement(cls, SqlMethod.UPDATE_BY_ID);
        Throwable var7 = null;
        int batchSize = entityList.size();

        try {
            int i = 0;

            for (Object entity : entityList) {
                MapperMethod.ParamMap<Object> param = new MapperMethod.ParamMap();
                param.put(Constants.ENTITY, entity);
                batchSqlSession.update(sqlStatement, param);

                if (i >= 1 && i % batchSize == 0) {
                    batchSqlSession.flushStatements();
                }

                i++;
            }

            batchSqlSession.flushStatements();
            return true;
        } catch (Throwable var20) {
            var7 = var20;
            throw var20;
        } finally {
            if (batchSqlSession != null) {
                if (var7 != null) {
                    try {
                        batchSqlSession.close();
                    } catch (Throwable var19) {
                        var7.addSuppressed(var19);
                    }
                } else {
                    batchSqlSession.close();
                }
            }

        }
    }
}
